package com.qire.manhua.view;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

/**
 *  NonScrollListView 不能滑动的ListView
 */
public class NonScrollListView extends LinearLayout {

    /**
     * 数据元素项目数
     */
    private int mItemCount;

    /**
     * 循环利用回收站
     */
    private final RecycleBin recycleBin = new RecycleBin();

    /**
     * 数据适配器
     */
    private BaseAdapter mAdapter;

    /**
     * 适配数据观察者
     */
    private AdapterDataSetObserver mDataSetObserver;

    public NonScrollListView(Context context) {
        this(context, null);
    }

    public NonScrollListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NonScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setAdapter(BaseAdapter adapter) {
        // 如果存在就注销，重新接受新的。这段如果引起异常也可以移除
        if(mAdapter != null && mDataSetObserver != null) {
            mAdapter.unregisterDataSetObserver(mDataSetObserver);
        }

        mAdapter = adapter;
        mItemCount = mAdapter == null ? 0 : mAdapter.getCount();

        if (mDataSetObserver == null) {
            mDataSetObserver = new AdapterDataSetObserver();
        }
        if (mAdapter != null) {
            mAdapter.registerDataSetObserver(mDataSetObserver);
        }
        fillData();
    }

    /**
     * 填充数据
     */
    private void fillData() {
        recycleBin.fillData();
        invalidate();
    }

    private class AdapterDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            mItemCount = mAdapter.getCount();
            fillData();
            requestLayout();
        }

        @Override
        public void onInvalidated() {
            mItemCount = 0;
            requestLayout();
        }
    }

    private final class RecycleBin {

        private RecycleBin() {}

        public void reset() {
            int childCount = getChildCount();
            if(childCount == mItemCount) {
                return;
            }
            if(childCount > mItemCount) {
                // 子控件多了
                removeViewsInLayout(mItemCount, childCount - mItemCount);
            } else {
                // 子控件少了

            }
        }

        public void fillData() {
            reset();
            int childCount = getChildCount();
            for(int index = 0; index < mItemCount; index++) {
                if(index < childCount) {
                    View view = getChildAt(index);
                    mAdapter.getView(index, view, NonScrollListView.this);
                } else {
                    View view = mAdapter.getView(index, null, NonScrollListView.this);
                    addViewInLayout(view,  -1, view.getLayoutParams(), true);
                }
            }
        }

    }

}